public class Test {
    int a, b;
    int[] dx = {0, 0, 1, -1, 1, -1, 1, -1};
    int[] dy = {1, -1, 0, 0, 1, -1, -1, 1};
    public char[][] updateBoard(char[][] board, int[] click) {
        a = board.length;
        b = board[0].length;
        int i = click[0], j = click[1];
        if(board[i][j] == 'M') {
            board[i][j] = 'X';
        } else {
            dfs(board, i, j);
        }
        return board;
    }
    void dfs(char[][] board, int i, int j) {
        int count = 0;
        for(int k = 0; k < 8; k++) {
            int x = i + dx[k], y = j + dy[k];
            if(x >= 0 && x < a && y >= 0 && y < b && board[x][y] == 'M') {
                count++;
            }
        }
        if(count == 0) {
            board[i][j] = 'B';
            for(int k = 0; k < 8; k++) {
                int x = i + dx[k], y = j + dy[k];
                if(x >= 0 && x < a && y >= 0 && y < b && board[x][y] == 'E') {
                    dfs(board, x, y);
                }
            }
        } else {
            board[i][j] = (char)('0' + count);
        }
    }
}
